﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Resources;
using System.Reflection;

namespace UserInterface
{
    using BusinessLogicLayer;
    using BusinessObjectLayer;
    using ComplexDataType;
    using JntMsgBox;
    using System.Net;
    using System.IO;
    using BusinessLogicLibrary;

    public partial class EditPaperUI : Form
    {
        private PaperBLL _PaperBLL;
        private PaperBOL _PaperBOL;
        private ResourceManager _ResourceManager = new ResourceManager("UserInterface.Properties.Resources", Assembly.GetExecutingAssembly());
        ExceptionHandlerBOL _ExceptionHandlerBOL;
        private FormMode formMode;
        string UpdateText;
        string DeleteText;
        string UpdateConfirmation;
        string DeleteConfirmation;

        public EditPaperUI()
        {
            InitializeComponent();
            editButton.Text = _ResourceManager.GetString("CreateButtonText");
            this.Text = _ResourceManager.GetString("AddNewPaper");
            paperTitleTextBox.Focus();
            deleteButton.Visible = false;
            formMode = FormMode.Insert;
            _PaperBLL = new PaperBLL();
        }
        public EditPaperUI(PaperBOL paperBOL)
        {
            InitializeComponent();
            formMode = FormMode.Update;
            paperTitleTextBox.Focus();
            _PaperBLL = new PaperBLL();
            _PaperBOL = paperBOL;
            fillTextBoxes(paperBOL);
        }
        private void SetResourceManager()
        {
            try
            {
                UpdateText = _ResourceManager.GetString("Update");
                DeleteText = _ResourceManager.GetString("Delete");
                UpdateConfirmation = _ResourceManager.GetString("UpdateConfirmation");
                DeleteConfirmation = _ResourceManager.GetString("DeleteConfirmation");
            }
            catch (Exception exception)
            {
                _ExceptionHandlerBOL = ExceptionHandlerBLL.HandleException(exception);
                noticeLabel.ForeColor = Color.Red;
                noticeLabel.Text = _ExceptionHandlerBOL.Title;
            }
        }

        private void fillTextBoxes(PaperBOL _PaperBOL)
        {
            paperIdTextBox.Text = _PaperBOL.PaperId.ToString();
            paperTitleTextBox.Text = _PaperBOL.PaperTitle;
            descriptionTextBox.Text = _PaperBOL.Description;
            imageURLTextBox.Text = _PaperBOL.ImageURL;
            fileURLTextBox.Text = _PaperBOL.FileURL;
        }
        private void cancelButton_Click(object sender, EventArgs e)
        {
            CloseForm();
        }
        private void CloseForm()
        {
            this.Close();
        }
        private void editButton_Click(object sender, EventArgs e)
        {
            if (ValidateItem())
                GetEditConfirmtion();
        }
        private bool ValidateItem()
        {
            bool isValid = true;
            if (paperTitleTextBox.Text == string.Empty)
            {
                reqErrorProvider.SetError(paperTitleTextBox, "*");
                isValid = false;
            }
            if (imageURLTextBox.Text == string.Empty)
            {
                reqErrorProvider.SetError(imageURLTextBox, "*");
                isValid = false;
            }
            if (descriptionTextBox.Text == string.Empty)
            {
                reqErrorProvider.SetError(descriptionTextBox, "*");
                isValid = false;
            }
            return isValid;
        }
        private void GetEditConfirmtion()
        {
            try
            {
                if (formMode == FormMode.Update)
                {
                    SetResourceManager();
                    JntMsgBoxFarsi _JntMsgBoxFarsi = new JntMsgBoxFarsi();
                    DialogResult result = _JntMsgBoxFarsi.Show(UpdateConfirmation, UpdateText, JntStyle.YesNo);
                    if (result == DialogResult.No)
                        return;
                }
                Edit();
                CloseForm();
            }
            catch (Exception exception)
            {
                _ExceptionHandlerBOL = ExceptionHandlerBLL.HandleException(exception);
                noticeLabel.ForeColor = Color.Red;
                noticeLabel.Text = _ExceptionHandlerBOL.Title;
            }
            finally { }
        }
        private void Edit()
        {
            try
            {
                short? paperId = null;
                try
                {
                    paperId = Convert.ToInt16(paperIdTextBox.Text);
                }
                catch
                {
                }
                string paperTitle = paperTitleTextBox.Text;
                string description = descriptionTextBox.Text;
                string fileName = openFileDialog.FileName.Substring(openFileDialog.FileName.LastIndexOf("\\")).Replace("\\","");
                string fileDestination = string.Format("{0}//Paper//{1}", ServerBLL.GetStringValue("FilePath"), fileName);
                string imageName = openImageDialog.FileName.Substring(openImageDialog.FileName.LastIndexOf("\\")).Replace("\\", "");
                string imageDestination = string.Format("{0}//Paper//{1}", ServerBLL.GetStringValue("ImagePath"), imageName);
                FileBLL _FileBLL = new FileBLL();
                _FileBLL.Write(new FileBOL(openFileDialog.FileName, fileDestination, false));
                _FileBLL.Write(new FileBOL(openImageDialog.FileName, imageDestination, false));
                PaperBOL _PaperBOL = new PaperBOL(paperId, paperTitle, description, fileName, imageName);
                if (formMode == FormMode.Insert)
                    _PaperBLL.Insert(_PaperBOL);
                else
                    _PaperBLL.Update(_PaperBOL);
            }
            catch (Exception exception)
            {
                _ExceptionHandlerBOL = ExceptionHandlerBLL.HandleException(exception);
                noticeLabel.ForeColor = Color.Red;
                noticeLabel.Text = _ExceptionHandlerBOL.Title;
            }
            finally { }
        }
        private void deleteButton_Click(object sender, EventArgs e)
        {
            GetDeleteConfirmtion();
        }
        private void GetDeleteConfirmtion()
        {
            try
            {
                SetResourceManager();
                JntMsgBoxFarsi _JntMsgBoxFarsi = new JntMsgBoxFarsi();
                DialogResult result = _JntMsgBoxFarsi.Show(DeleteConfirmation, DeleteText, JntStyle.YesNo);
                if (result == DialogResult.No)
                    return;
                _PaperBLL.Delete(_PaperBOL);
                CloseForm();
            }
            catch (Exception exception)
            {
                _ExceptionHandlerBOL = ExceptionHandlerBLL.HandleException(exception);
                noticeLabel.ForeColor = Color.Red;
                noticeLabel.Text = _ExceptionHandlerBOL.Title;
            }
            finally { }
        }
        private void noticeLabel_Click(object sender, EventArgs e)
        {
            if (_ExceptionHandlerBOL != null)
                new ExceptionHandlerUI(_ExceptionHandlerBOL).ShowDialog();
            _ExceptionHandlerBOL = null;
        }
        private void setFarsiKeyboard_Enter(object sender, EventArgs e)
        {
            try
            {
                KeyboardControler.SetFarsiKeyboard();
            }
            catch (Exception exception)
            {
                _ExceptionHandlerBOL = ExceptionHandlerBLL.HandleException(exception);
                noticeLabel.ForeColor = Color.Red;
                noticeLabel.Text = _ExceptionHandlerBOL.Title;
            }
        }
        private void imageURLButton_Click(object sender, EventArgs e)
        {
            try
            {
                DialogResult dialogResult = openImageDialog.ShowDialog();
                if (dialogResult == System.Windows.Forms.DialogResult.OK)
                    imageURLTextBox.Text = openImageDialog.FileName;
            }
            catch (Exception exception)
            {
                _ExceptionHandlerBOL = ExceptionHandlerBLL.HandleException(exception);
                noticeLabel.ForeColor = Color.Red;
                noticeLabel.Text = _ExceptionHandlerBOL.Title;
            }
        }
        private void fileURLButton_Click(object sender, EventArgs e)
        {
            try
            {
                if (openFileDialog.ShowDialog() == System.Windows.Forms.DialogResult.OK)
                    fileURLTextBox.Text = openFileDialog.FileName;
            }
            catch (Exception exception)
            {
                _ExceptionHandlerBOL = ExceptionHandlerBLL.HandleException(exception);
                noticeLabel.ForeColor = Color.Red;
                noticeLabel.Text = _ExceptionHandlerBOL.Title;
            }
        }
    }
}